package cn.fl.project.service.impl;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.project.service.IPrjLnkCompApplyService;
import cn.fl.project.transform.PrjLnkCompApplyVOTransform;
import cn.fl.project.vo.PrjLnkCompApplyVO;
import fl.spl.facade.lnkcomp.api.IPrjLcApplyRlPsnApiService;
import fl.spl.facade.lnkcomp.api.IPrjLcApplyRlPsnQueryApiService;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompApplyApiService;
import fl.spl.facade.lnkcomp.api.IPrjLnkCompApplyQueryApiService;
import fl.spl.facade.lnkcomp.condition.PrjLcApplyRlPsnQueryCondition;
import fl.spl.facade.lnkcomp.condition.PrjLnkCompApplyQueryCondition;
import fl.spl.facade.lnkcomp.dto.PrjLcApplyRlPsnDTO;
import fl.spl.facade.lnkcomp.dto.PrjLnkCompApplyDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 挂靠公司变更申请表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjLnkCompApplyServiceImpl implements IPrjLnkCompApplyService {
	
	/**
	 * 项目API 暴露服务
	 */
	@Autowired
	private IPrjLnkCompApplyApiService prjLnkCompApplyApiService;
	
	@Autowired
	private IPrjLcApplyRlPsnApiService prjLcApplyRlPsnApiService;

	@Autowired
	private IPrjLnkCompApplyQueryApiService prjLnkCompApplyQueryApiService;

	@Autowired
	private IPrjLcApplyRlPsnQueryApiService prjLcApplyRlPsnQueryApiService;

	@Override
	public PrjLnkCompApplyVO selectById(Long id) throws BaseException {
		if(null == id){
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		};
		PrjLnkCompApplyDTO dto = this.prjLnkCompApplyQueryApiService.selectById(id);
		if(dto != null){
			return PrjLnkCompApplyVOTransform.toVO(dto);
		}else{
			return null ;
		}
	}

	@Override
	public Long add(PrjLnkCompApplyVO entity) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		};
		//系统日志-有用户的情况下
		// 新增挂靠公司关联人员变更数据
		PrjLnkCompApplyDTO dto = PrjLnkCompApplyVOTransform.toDTO(entity);
		Long add = this.prjLnkCompApplyApiService.add(dto);
		return add;
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		if(StringUtils.isEmpty(id)) {
			return false;
		}
		return this.prjLnkCompApplyApiService.removeById(id);
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean modify(PrjLnkCompApplyVO entity) throws BaseException {
		if(null == entity){
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		};
		return this.prjLnkCompApplyApiService.modify(PrjLnkCompApplyVOTransform.toDTO(entity));
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		Pager pager = this.prjLnkCompApplyQueryApiService.searchListPage(page);
		if(CollectionUtils.isNotEmpty(page.getResultList())){
			pager.setResultList(PrjLnkCompApplyVOTransform.toVOList((List<PrjLnkCompApplyDTO>) page.getResultList()));
		}
		return pager;
	}
	
	@Override
    public List<PrjLnkCompApplyVO> searchList(PrjLnkCompApplyQueryCondition condition) throws BaseException{
		if(null == condition){
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.COMMON_0001_NAME);
		};
		List<PrjLnkCompApplyDTO> list = this.prjLnkCompApplyQueryApiService.searchList(condition);
		return PrjLnkCompApplyVOTransform.toVOList(list);
	}

	@Override
	public boolean deleteFailDataById(Long id) throws BaseException {
		if(null == id){
			throw new BaseException(BaseExceptionConstant.COMMON_0001,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		boolean removeById = this.prjLnkCompApplyApiService.removeById(id);
		PrjLcApplyRlPsnQueryCondition qc = new PrjLcApplyRlPsnQueryCondition();
		qc.setPrjLnkCompApplyId(id);
		List<PrjLcApplyRlPsnDTO> searchList = this.prjLcApplyRlPsnQueryApiService.searchList(qc);
		int i=0;
		for(PrjLcApplyRlPsnDTO prjId :searchList){
			this.prjLcApplyRlPsnApiService.removeById(prjId.getId());
			i++;
		}
		if(i!=searchList.size() || removeById!=true){
			throw new BaseException(BaseExceptionConstant.COMMON_0002,BaseExceptionConstant.COMMON_0002_NAME);
		}
		return true;
	}

	@Override
	public boolean checkFlowStatus(Long id, String sysCd, String orgCd) {
		return this.prjLnkCompApplyQueryApiService.checkFlowStatus(id,sysCd,orgCd);
	}

}
